<template>
  <el-dialog v-model="visible" :title="dataForm.row.teamName+'-'+dataForm.row.company+'-主领队'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" label-width="120px">
      <el-form-item prop="realName" label="姓名">
        {{dataForm.row.realName}}
      </el-form-item>
      <el-form-item prop="sex" label="性别">
        {{dataForm.row.sex}}
      </el-form-item>
      <el-form-item prop="company" label="所属单位">
        {{dataForm.row.company}}
      </el-form-item>
      <el-form-item prop="phone" label="电话">
        {{dataForm.row.phone}}
      </el-form-item>
      <el-form-item prop="header" label="一寸照片">
        <img width="100" height="140" :src="dataForm.row.header" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import { isMobile, useDebounce } from "@/utils/utils";
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    const state = reactive({
      visible: false,
      dataForm: {
        id: "",
        smsCode: "",
        mobile: "",
        params: "",
        row: {
          teamName: "",
          account: "",
          phone: "",
          email: "",
          realName: "",
          idType: "",
          idCard: "",
          sex: "",
          company: "",
          roleId: "",
          header: "",
          empower: ""
        }
      },
    });
    return { ...useView(state), ...toRefs(state) };
  },
  computed: {
    dataRule() {
      var validateMobile = (rule: IObject, value: string, callback: (e?: Error) => any) => {
        if (!isMobile(value)) {
          return callback(new Error(this.$t("validate.format", { attr: this.$t("user.mobile") })));
        }
        callback();
      };
      return {
        mobile: [
          { required: true, message: this.$t("validate.required"), trigger: "blur" },
          { validator: validateMobile, trigger: "blur" }
        ]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
      });
      console.log(this.dataForm.row);
    },
    /**
     * 身份证隐藏中间十位
     */
    hideIdCard(idCard: string): string {
      //验证身份证号必须满足18位，最后一位可以是字母
      if (!/^\d{17}(\d|x)$/i.test(idCard)) {
        return idCard;
      }else{
        return idCard.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
      }
    },

    userType(value?: string) {
      let result = '';
      if(value && value.includes('0')){
        result += ' 代表队负责人';
      }
      if(value && value.includes('1')){
        result += ' 领队';
      }
      if(value && value.includes('2')){
        result += ' 指导老师';
      }
      if(value && value.includes('3')){
        result += ' 选手';
      }
      return result;
    },

    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        baseService
          .post("/sys/sms/send", this.dataForm, {
            "content-type": "application/x-www-form-urlencoded"
          })
          .then((res) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                this.visible = false;
                this.$emit("refreshDataList");
              }
            });
          });
      });
    }
  }
});
</script>
